function [grid_struct, status] = rslice_xyzm ( rslice_struct )
% RSLICE_XYZM:  gets x, y, z, and mask grid for a particular variable
%
% USAGE:  
% PARAMETERS
% Input:
%     rslice_struct:
%         Structure of rslice fields.
% Output:
%     grid structure with fields x, y, z, m
%         X coords, y coords, bathymetry, and mask
%     status:  
%         -1 if the routine fails.  
%         -2 if the named variable is inappropriate
%

x = [];
y = [];
z = [];
status = -1;

%
% check that we didn't get a singleton variable
if length(rslice_struct.varinfo.Size) == 0
	msg = sprintf ( '%s:  variable %s is a singleton variable. .\n', mfilename, varname, ncfile );
	rslice_warning ( msg );
	status = -2;
	return
end


%
% Is it a rho, u, or v variable?
is_u_variable = 0;
is_v_variable = 0;
is_rho_variable = 0;
ndims = length(rslice_struct.varinfo.Dimension);
for j = 1:ndims
	switch ( rslice_struct.varinfo.Dimension{j} )
	case 'eta_u'
		is_u_variable = 1;
	case 'eta_v'
		is_v_variable = 1;
	case 'eta_rho'
		is_rho_variable = 1;
	end
end

gridx = rslice_struct.rslice_grid.x.data;
gridy = rslice_struct.rslice_grid.y.data;
if isfield ( rslice_struct.rslice_grid.roms_grid, 'h' )
    gridz = rslice_struct.rslice_grid.roms_grid.h;
else
    gridz = 0;
end

if isfield ( rslice_struct.rslice_grid.roms_grid, 'mask_rho' )
	gridm = rslice_struct.rslice_grid.roms_grid.mask_rho;
else
	gridm = ones(size(gridx));
end

[r,c] = size(rslice_struct.rslice_grid.x.data);
if is_rho_variable
	x = gridx;
	y = gridy;
	z = gridz;
	m = gridm;
elseif is_u_variable
	x = ( gridx(:,1:end-1) + gridx(:,2:end) ) / 2;
	y = ( gridy(:,1:end-1) + gridy(:,2:end) ) / 2;
	z = ( gridz(:,1:end-1) + gridz(:,2:end) ) / 2;
	m = ( gridm(:,1:end-1) + gridm(:,2:end) ) / 2;
elseif is_v_variable
	x = ( gridx(1:end-1,:) + gridx(2:end,:) ) / 2;
	y = ( gridy(1:end-1,:) + gridy(2:end,:) ) / 2;
	z = ( gridz(1:end-1,:) + gridz(2:end,:) ) / 2;
	m = ( gridm(1:end-1,:) + gridm(2:end,:) ) / 2;
else
	msg = sprintf ( '%s:  could not figure whether %s was a rho, u, or v variable', mfilename, varname );
	rslice_error ( msg );
end

grid_struct.x = x;
grid_struct.y = y;
grid_struct.z = z;
grid_struct.mask = m;
status = 0;

return
